<?php
require("admin-header.php");
require_once("../include/set_get_key.php");

if(!(isset($_SESSION[$OJ_NAME.'_'.'administrator']) || isset($_SESSION[$OJ_NAME.'_'.'problem_editor']))){
  echo "<a href='../loginpage.php'>请先登录！</a>";
  exit(1);
}

if(isset($OJ_LANG)){
  require_once("../lang/$OJ_LANG.php");
}
?>

<title>Problem List</title>
<?php include("template/$OJ_TEMPLATE/css.php");?>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="http://cdn.bootcss.com/html5shiv/3.7.0/html5shiv.js"></script>
<script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->



<div class='container'>
<h2><?php echo "题目列表"?></h2>
    <br>
<?php
$sql = "SELECT COUNT('problem_id') AS ids FROM `problem`";
$result = pdo_query($sql);
$row = $result[0];

$ids = intval($row['ids']);

$idsperpage = 50;
$pages = intval(ceil($ids/$idsperpage));

if(isset($_GET['page'])){ $page = intval($_GET['page']);}
else{ $page = 1;}

$pagesperframe = 5;
$frame = intval(ceil($page/$pagesperframe));

$spage = ($frame-1)*$pagesperframe+1;
$epage = min($spage+$pagesperframe-1, $pages);

$sid = ($page-1)*$idsperpage;

$sql = "";
if(isset($_GET['keyword']) && $_GET['keyword']!=""){
  $keyword = $_GET['keyword'];
  $keyword = "%$keyword%";
  $sql = "SELECT `problem_id`,`title`,`accepted`,`in_date`,`defunct` FROM `problem` WHERE (problem_id LIKE ?) OR (title LIKE ?) OR (description LIKE ?) OR (source LIKE ?)";
  $result = pdo_query($sql,$keyword,$keyword,$keyword,$keyword);
}else{
  $sql = "SELECT `problem_id`,`title`,`accepted`,`in_date`,`defunct` FROM `problem` ORDER BY `problem_id` DESC LIMIT $sid, $idsperpage";
  $result = pdo_query($sql);
}
?>


<form action=problem_list.php class="form-search form-inline">
    <p style="display: inline;font-size: 16px">搜索关键词： </p>
  <input type="text" name=keyword class="form-control search-query" style="width: 600px" placeholder="<?php echo $MSG_PROBLEM_ID.', '.$MSG_TITLE.', '.$MSG_Description.', '.$MSG_SOURCE?>">
  <button type="submit" class="form-control">搜索</button>
</form>
    <br>


<?php
/*
echo "<select class='input-mini' onchange=\"location.href='problem_list.php?page='+this.value;\">";
for ($i=1;$i<=$cnt;$i++){
        if ($i>1) echo '&nbsp;';
        if ($i==$page) echo "<option value='$i' selected>";
        else  echo "<option value='$i'>";
        echo $i+9;
        echo "**</option>";
}
echo "</select>";
*/
?>


    <form method=post action=contest_add.php>
        <input type=submit name='problem2contest' class="btn btn-success" value='勾选题目作为作业'>&nbsp;&nbsp;
        <a href='problem_add_page.php'><button type='button' class='btn btn-primary'>添加题目</button></a>
<!--        可以用-->
<!--        <style type="text/css">-->
<!--            .table-hover>tbody>tr:hover>td {-->
<!--                background-color: #98FB9825;-->
<!--            }-->
<!--        </style>-->
<!--        <table class='table table-hover'  width='90%' style="text-align:center;">-->
<table class='table table-striped'  width='90%' style="text-align:center;">

      <thead>
        <tr>
          <td width=150px><input type=checkbox onchange='$("input[type=checkbox]").prop("checked", this.checked)'>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $MSG_PROBLEM_ID?></td>
          <td><?php echo $MSG_TITLE?></td>
    <!--      <td>--><?php //echo $MSG_AC?><!--</td>-->
          <td>创建时间</td>
          <?php
          if(isset($_SESSION[$OJ_NAME.'_'.'administrator']) ||isset($_SESSION[$OJ_NAME.'_'.'problem_editor'])){
            if(isset($_SESSION[$OJ_NAME.'_'.'administrator']) ||isset($_SESSION[$OJ_NAME.'_'.'problem_editor']))
              echo "<td>题目状态</td><td style='text-align: center'>操作</td>";
          }
          ?>
        </tr>
      </thead>
    <tbody>
    <?php
    foreach($result as $row){
      echo "<tr>";
        echo "<td><input type=checkbox name='pid[]' value='".$row['problem_id']."'>&nbsp;&nbsp;&nbsp;&nbsp;".$row['problem_id']."</td>";
        echo "<td><a href='../problem.php?id=".$row['problem_id']."'>".$row['title']."</a></td>";
//        echo "<td>".$row['accepted']."</td>";
        echo "<td>".$row['in_date']."</td>";
        if(isset($_SESSION[$OJ_NAME.'_'.'administrator'])||isset($_SESSION[$OJ_NAME.'_'.'problem_editor'])){
          if(isset($_SESSION[$OJ_NAME.'_'.'administrator']) || isset($_SESSION[$OJ_NAME.'_'.'problem_editor'])){
            echo "<td><a href=problem_df_change.php?id=".$row['problem_id']."&getkey=".$_SESSION[$OJ_NAME.'_'.'getkey'].">".($row['defunct']=="N"?"<span titlc='click to reserve it' class=green>已启用</span>":"<span class=red title='click to be available'>未启用</span>")."</a><td style='text-align: center;'>";
            if($OJ_SAE||function_exists("system")){
    ?>
              <button type='button' class='btn btn-danger btn-sm'  onclick='javascript:if(confirm("Delete?")) location.href="problem_del.php?id=<?php echo $row['problem_id']?>&getkey=<?php echo $_SESSION[$OJ_NAME.'_'.'getkey']?>"'><i class='glyphicon glyphicon-trash'></i>删除</button>

        <?php
        }
      }
      if(isset($_SESSION[$OJ_NAME.'_'.'administrator']) || isset($_SESSION[$OJ_NAME.'_'."p".$row['problem_id']]) ||isset($_SESSION[$OJ_NAME.'_'.'problem_editor'])){
        echo "<a href=problem_edit.php?id=".$row['problem_id']."&getkey=".$_SESSION[$OJ_NAME.'_'.'getkey'].">";
//          style='float:left'
          echo "<button type='button' class='btn btn-primary btn-sm'  ><i class='glyphicon glyphicon-edit'></i>编辑</button></a> ";
          echo "<a href='javascript:phpfm(".$row['problem_id'].");'>";
        echo "<button type='button' class='btn btn-info btn-sm'><i class='glyphicon glyphicon-edit'></i>修改测试用例</button></a>";
      }
    }
    echo "</tr>";
  }
?>
    </tbody>
<!--    <tr>-->
<!--      <td colspan=2 style="height:40px;">勾选题目</td>-->
<!--      <td colspan=6>-->
<!--        <input type=submit name='problem2contest' value='创建新的作业'>-->
<!--        <input type=submit name='enable' value='启用所选题目' onclick='$("form").attr("action","problem_df_change.php")' >-->
<!--        <input type=submit name='disable' value='停用所选题目' onclick='$("form").attr("action","problem_df_change.php")'>-->
<!--      </td>-->
<!--    </tr>-->

</table>

    </form>


<script src='../template/bs3/jquery.min.js' ></script>

<script>
function phpfm(pid){
  //alert(pid);
  $.post("phpfm.php",{'frame':3,'pid':pid,'pass':''},function(data,status){
    if(status=="success"){
      document.location.href="phpfm.php?frame=3&pid="+pid;
    }
  });
}
</script>
</div>

<?php
if(!(isset($_GET['keyword']) && $_GET['keyword']!=""))
{
  echo "<div style='display:inline;'>";
  echo "<nav class='center'>";
  echo "<ul class='pagination pagination-sm'>";
  echo "<li class='page-item'><a href='problem_list.php?page=".(strval(1))."'>&lt;&lt;</a></li>";
  echo "<li class='page-item'><a href='problem_list.php?page=".($page==1?strval(1):strval($page-1))."'>&lt;</a></li>";
  for($i=$spage; $i<=$epage; $i++){
    echo "<li class='".($page==$i?"active ":"")."page-item'><a title='go to page' href='problem_list.php?page=".$i.(isset($_GET['my'])?"&my":"")."'>".$i."</a></li>";
  }
  echo "<li class='page-item'><a href='problem_list.php?page=".($page==$pages?strval($page):strval($page+1))."'>&gt;</a></li>";
  echo "<li class='page-item'><a href='problem_list.php?page=".(strval($pages))."'>&gt;&gt;</a></li>";
  echo "</ul>";
  echo "</nav>";
  echo "</div>";
}
?>

</div>
